package com.hyzx.qbasic.admin.domain.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.OrgInfoPO;
import com.hyzx.qbasic.admin.model.po.OrgPO;
import com.hyzx.qbasic.admin.domain.dao.OrgMapper;
import com.hyzx.qbasic.admin.domain.repository.IOrgRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 黄江华 on 2017/10/9
 */

@Repository
public class OrgRepositoryImpl implements IOrgRepository {

    @Autowired
    private OrgMapper orgMapper;

    @Override
    public List<OrgPO> getAllOrgList() {
        return orgMapper.getAllOrgList();
    }

    @Override
    public PageInfo<OrgInfoPO> getPagedOrgList(int pageNum, int pageSize, String orgName, Integer orgId) {
        PageInfo pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> orgMapper.getPagedOrgList(orgName, orgId));

        return pageInfo;
    }

    @Override
    public boolean addOrg(OrgPO org) {
        return orgMapper.addOrg(org) > 0;
    }

    @Override
    @Transactional
    public boolean deleteOrg(List<Integer> orgIdList) {
        //级联删除组织机构及其子组织机构
        int deleteOrgResult = orgMapper.deleteOrg(orgIdList);
        orgMapper.deleteOrgUsers(orgIdList);

        return deleteOrgResult > 0;
    }

    @Override
    public OrgPO getOrgInfo(int orgId) {
        return orgMapper.getOrgInfo(orgId);
    }

    @Override
    public boolean editOrg(OrgPO org) {
        return orgMapper.editOrg(org) > 0;
    }

    @Override
    public Integer getUserOrgId(int userId) {
        return orgMapper.getUserOrgId(userId);
    }
}
